package com.ydm.myapp001.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.ydm.myapp001.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by yangdongming on 2017/12/28.
 */

public class Second001Activity extends AppCompatActivity {

    private static final String TAG = "YDMLogSecond001Activity";
    private ListView myListView;//列表
    private ArrayList<String> itemList;//数据（用于ArrayAdapter适配器）
    private ArrayList<HashMap<String,Object>> data;//数据 (用于SimpleAdapter适配器)
    private final String TITLE = "title";
    private final String IMAGE = "image";
    private final String DATA = "data";
    private Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second001);
        mContext = this;

        /*
        //1.listView装载ArrayAdapter适配器
        loadArrayAdapter();
        */


        //2.listView装载SimpleAdapter适配器
        loadSimpleAdapter();
    }


    public void loadArrayAdapter() {
        /*
        * 数据源model
        * */
        itemList = new ArrayList<String>();
        for(int i=0;i<20;i++){
            itemList.add("测试数据"+i);
        }


        /*
        * ArrayAdapter适配器（ArrayAdapter的参数如下：）
        * Context 指定现在的上下文
        * int Resource 指定一个行布局文件，显示每行的显示
        * List<> Objects 在list上显示的数据信息
        * */
        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itemList);//适配器


        /*
        * 装载适配器
        * */
        myListView = (ListView) findViewById(R.id.listView);
        myListView.setAdapter(myAdapter);
    }



    public void loadSimpleAdapter() {
        /*
        * 数据源model
        * */
        data = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < 20; i++) {
            HashMap<String, Object> mapItem = new HashMap<String, Object>();
            mapItem.put(TITLE, "标题"+i);
            if (i % 3 == 0)
                mapItem.put(DATA, "开始内容介绍内容介绍内容介绍结束"+i);
            else
                mapItem.put(DATA, "开始内容介绍内容介绍内容介绍内容介绍内容介绍内容介绍内容介绍内容介绍内容介绍结束"+i);
            if (i % 2 == 0)
                mapItem.put(IMAGE, R.drawable.test_img001);
            else
                mapItem.put(IMAGE, R.drawable.test_img002);

            data.add(mapItem);
        }


        /*
        * SimpleAdapter（SimpleAdapter的参数如下：）
        * Context 指定现在的上下文
        * int Resource 指定一个行布局文件，显示每行的显示
        * List<> Objects 在list上显示的数据信息
        * */
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.item_listsimple, new String[]{TITLE,DATA,IMAGE}, new int[]{R.id.item_title, R.id.item_data, R.id.item_image});


        /*
        * 装载适配器
        * */
        myListView = (ListView) findViewById(R.id.listView);
        myListView.setAdapter(simpleAdapter);
        
        
        
        /*
        * 点击列表某行的响应
        * */
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.d(TAG, "onItemClick: 点击了 " + i);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: Second001Activity");
    }

    public void backToHomePage (View v) {
        Intent intent = new Intent(mContext, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}
